# -*- coding: utf-8 -*-
from pipe import Pipe
import threading
from req import  Req
import Util#Util.getDict
import random
from factoryReq import  ReqFactory
from mongoBase import MongoDBBase
from factoryFetcher import Fetcher
import units
from factoryUnit import UnitFactory
from saver import Saver
TAG_LIST_COLL=[("tag_list_id",int),("tag_id",int),("ans",int),("ace",int)]

'''
for a field "ans" ---> 1 is True ,-1 is False ,0 is not exist
(for set 1 and set 2)
pic_user_coll
{"uid":1,"pid":1,"tag_list_id":1}
{"uid":1,"pid":2,"tag_list":2}
tag_list_coll(uid+pid-->tag_list_id)
{"tag_list_id":1,"tag_id":1,"ans":True,"ace":Ture/False/None}
{"tag_list_id":1,"tag_id":2,"ans":True,"ace":Ture/False/None}
{"tag_list_id":1,"tag_id":3,"ans":True,"ace":Ture/False/None}
{"tag_list_id":1,"tag_id":4,"ans":True,"ace":Ture/False/None}
{"tag_list_id":1,"tag_id":5,"ans":True,"ace":Ture/False/None}
{"tag_list_id":2,"tag_id":1","ans":True,"ace":Ture/False/None}
{"tag_list_id":2,"tag_id":2,"ans":True,"ace":Ture/False/None}
{"tag_list_id":2,"tag_id":3,"ans":True,"ace":Ture/False/None}
{"tag_list_id":2,"tag_id":4,"ans":True,"ace":Ture/False/None}
{"tag_list_id":2,"tag_id":5,"ans":True,"ace":Ture/False/None}

tag_pic_coll (for set 3)
{"tag":"isPlane",pid:"1","yesNum":20,"noNum":10}
{"tag":"isPlane",pid:"2","yesNum":21,"noNum":30}
{"tag":"isPlane",pid:"3","yesNum":23,"noNum":6}

pic_coll
{"pid":1,"pic":"Plane"}
{"pid":2,"pic":"Train"}
tag_coll
{"tag_id":1,"tag":"isPlane"}
{"tag_id":2,"tag":"isTrain"}
{"tag_id":3,"tag":"isShip"}
{"tag_id":4,"tag":"isCar"}
{"tag_id":5,"tag":"isBike"}
'''
def mk_doc(scheme,num):
    new_doc = {}
    for field in scheme:
        fieldname = field[0]
        fieldtype = field[1]
        new_doc[fieldname] = random.choice([1, 2, 3,4,5])
    return new_doc
def test_mk_doc(coll):
    for pid in range(100):
        pid=100000000+pid
        for uid in range(100):
            uid=uid
            for tid in range(5):
                tid=tid
                ans=random.choice([-1,0,1])
                ase = random.choice([-1, 0, 1])
                doc=Util.getDict(pid=pid,uid=uid,tid=tid,ans=ans,ase=ase)
                coll.insert_one(doc)



if __name__ == '__main__':
    ######Query Test
    #db.getCollection('uid_pid_tid_ans_ase_coll').find({"$or":[{"uid":1,"pid":100000001,"tid":1},{"uid":2,"pid":100000001,"tid":1},{"uid":3,"pid":100000001,"tid":1}]})
    myReq_list = []
    for i in range(5):
        input_info = {}
        input_info["unit_name"] = "SampleJudge"
        input_info["dataset_from_commit"] = {}
        input_info["dataset_from_commit"]["dataset_usage"] = "dataset_from_commit"
        input_info["dataset_from_commit"]["field_name_list"] = ['uid', 'pid', 'tid', 'ans']
        input_info["dataset_from_commit"]["uid_pid_tid_ans_list"] = []
        for j in range(10):
            input_info["dataset_from_commit"]["uid_pid_tid_ans_list"].append(Util.getDict(
                uid=i + 1, pid=100000001 + j // 4, tid=j % 4 + 1, ans=random.choice([-1, 0, 1]), ase=0))
        print(input_info)
        req = Req(id=i + 1, input_info=input_info)
        myReq_list.append(req)

    user_to_fetcher_pipe = Pipe()
    myReqFactory = ReqFactory("myReqFactory", user_to_fetcher_pipe)
    for r in myReq_list:
        myReqFactory.work(r)
    for r in myReq_list:
        print(r.fetch_info)

    myconn = MongoDBBase()
    """
    ###### once
    ###### mk docs
    coll=myconn.get_coll("uid_pid_tid_ans_ase_coll")
    for uid in range(10):
        for pid in range(20):
            for tid in range(4):
                doc=Util.getDict(
                    uid=uid + 1, pid=100000001 + pid , tid=tid+1, ans=random.choice([-1, 0, 1]), ase=0)
                coll.insert_one(doc)

    coll = myconn.get_coll("tid_pid_num_coll")
    for pid in range(10):
        for tid in range(20):
            doc=Util.getDict(
                pid=100000001 + pid , tid=tid+1, yes_num=random.choice(range(20)), no_num=random.choice(range(20)))
            coll.insert_one(doc)
    """
    ###### mk docs

    fetcher_to_unit_pipe = Pipe()
    myFetcher1 = Fetcher("myFetcher1", user_to_fetcher_pipe=user_to_fetcher_pipe,
                         fetcher_to_unit_pipe=fetcher_to_unit_pipe, DB_conn=myconn)
    myFetcher1.start()
    myFetcher1.join()
    '''
       len=fetcher_to_unit_pipe.get_size()
    idx=0
    while idx<len:
        req=fetcher_to_unit_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now fetcher_to_unit_pipe has %d req left"%fetcher_to_unit_pipe.get_size())
        idx+=1

    '''

    unit_to_saver_pipe = Pipe()
    myUnitFactory = UnitFactory("myUnit1", fetcher_to_unit_pipe=fetcher_to_unit_pipe,
                                unit_to_saver_pipe=unit_to_saver_pipe)
    myUnitFactory.start()
    myUnitFactory.join()
    '''
        len = unit_to_saver_pipe.get_size()
    idx = 0
    while idx < len:
        req = unit_to_saver_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now unit_to_saver_pipe has %d req left" % unit_to_saver_pipe.get_size())
        idx += 1

    '''
    mySaver=Saver("mySaver1",unit_to_saver_pipe=unit_to_saver_pipe,DB_conn=myconn)
    mySaver.start()
    mySaver.join()


